
$(function() {
    validateRule();
    $('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
});

$.validator.setDefaults({
    submitHandler: function() {
    	register();
    }
});

function register() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var custName = $.common.trim($("input[name='custName']").val());
    var custPhone = $.common.trim($("input[name='custPhone']").val());
    var custId = $.common.trim($("input[name='custId']").val());
    var custAddr = $.common.trim($("input[name='custAddr']").val());
    var custSex = $.common.trim($("#custSex").val());
    var custPassword = $.common.trim($("input[name='custPassword']").val());
    var validateCode = $("input[name='validateCode']").val();
    $.ajax({
        type: "post",
        url: ctx + "register",
        data: {
            "custName": custName,
            "custPhone": custPhone,
            "custId": custId,
            "custAddr": custAddr,
            "custSex": custSex,
            "custPassword": custPassword,
            "validateCode": validateCode
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
            	layer.alert("<font color='red'>恭喜"+ custName + "，您的账号 " + custPhone + " 注册成功！</font>", {
            	    icon: 1,
            	    title: "系统提示"
            	},
            	function(index) {
            	    //关闭弹窗
            	    layer.close(index);
            	    location.href = ctx + 'login';
            	});
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#registerForm").validate({
        rules: {
            custName: {
                required: true,
                minlength: 2
            },
            custPhone: {
                required: true,
                minlength: 11
            },
            custId: {
                required: true,
                minlength: 6
            },
            custAddr: {
                required: true,
                minlength: 2
            },
            custPassword: {
                required: true,
                minlength: 6
            },
            confirmPassword: {
                required: true,
                equalTo: "[name='custPassword']"
            },
            validateCode: {
                required: true,
            }
        },
        messages: {
            custName: {
                required: icon + "请输入您的姓名",
                minlength: icon + "姓名不能小于2个字符"
            },
            custPhone: {
                required: icon + "请输入您的手机号",
                minlength: icon + "手机号不能小于11个字符"
            },
            custId: {
                required: icon + "请输入您的身份证号",
                minlength: icon + "姓名不能小于6个字符"
            },
            custAddr: {
                required: icon + "请输入您的住址",
                minlength: icon + "住址不能小于2个字符"
            },
            custPassword: {
            	required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于6个字符",
            },
            confirmPassword: {
                required: icon + "请再次输入您的密码",
                equalTo: icon + "两次密码输入不一致"
            },
            validateCode: {
                required: icon + "请输入验证码"
            }
        }
    })
}
